package com.blueheart.music.service.impl;

import com.blueheart.music.exception.BaseException;
import com.blueheart.music.exception.BaseExceptionEnum;
import com.blueheart.music.mapper.SingerMapper;
import com.blueheart.music.model.pojo.Singer;
import com.blueheart.music.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SingerServiceImpl implements SingerService {
    @Autowired
    private SingerMapper singerMapper;
    /**
     * 增
     *
     * @param singer
     */
    @Override
    public Boolean addSinger(Singer singer) {
        return singerMapper.insertSelective(singer)>0;
    }

    /**
     * 删
     *
     * @param id
     */
    @Override
    public Boolean removeSinger(Integer id) {
        return singerMapper.deleteByPrimaryKey(id)>0;
    }

    /**
     * 更新
     *
     * @param singer
     */
    @Override
    public Boolean reviseSinger(Singer singer) {
        return singerMapper.updateByPrimaryKeySelective(singer)>0;
    }

    /**
     * 查询全部歌手
     */
    @Override
    public List<Singer> findAll() {
        return singerMapper.selectAll();
    }

    /**
     * 通过名字模糊查询
     *
     * @param name
     */
    @Override
    public List<Singer> findByName(String name) {
        return singerMapper.selectByName(name);
    }

    /**
     * 通过性别查询歌手
     *
     * @param sex
     */
    @Override
    public List<Singer> findBySex(Byte sex) {
        return singerMapper.selectBySex(sex);
    }

    /**
     * 通过id查询歌手
     *
     * @param id
     */
    @Override
    public Singer findById(Integer id) {
        return singerMapper.selectByPrimaryKey(id);
    }

    /**
     * 重名判断
     *
     * @param name
     */
    @Override
    public Singer checkName(String name) {
        Singer singer = singerMapper.selectName(name);
        if (singer!=null){
            throw  new BaseException(BaseExceptionEnum.SINGER_EXIST);
        }
        return singer;
    }

    /**
     * 通过name查询歌手id
     *
     * @param name
     */
    @Override
    public Singer findToId(String name) {
        return singerMapper.selectName(name);
    }
}
